package com.cmsky.service.third.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cmsky.dao.CmsNodeContentDAO;
import com.cmsky.dao.CmsNodeDAO;
import com.cmsky.model.CmsNode;
import com.cmsky.model.CmsNodeContent;
import com.cmsky.model.CmsNodeContentExample;
import com.cmsky.model.CmsNodeExample;
import com.cmsky.model.CmsNodeExample.Criteria;
import com.cmsky.service.third.CmsQueryService;

@Service("cmsQueryService")
public class CmsQueryServiceImpl implements CmsQueryService {
	private static String className =  CmsQueryService.class.getName();
	private static Logger CmsQueryServiceLogger = Logger.getLogger(className);
	
	@Autowired
	private CmsNodeDAO cmsNodeDao;
	
	@Autowired
	private CmsNodeContentDAO cmsNodeContentDAO;
	
	public List<CmsNode> findNodesByPid(Integer pid, boolean link) {
		List<CmsNode> cmsNodeList = null;
		CmsNodeExample example = new CmsNodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andPidEqualTo(pid);
		cmsNodeList = this.cmsNodeDao.selectByExample(example);
		
		if(link){
			for(CmsNode node: cmsNodeList){
				node.setConentList(this.findNodeContentsById(node.getId()));
			}
		}
		CmsQueryServiceLogger.info("<|>"+className+"<|>findNodesByPid<|>{pid:"+pid+",link:"+link+"}<|>"+cmsNodeList.size()+"<|>");
		return cmsNodeList;
	}

	public CmsNode findNodeById(Integer id, boolean link) {
		CmsNode cmsNode = null;
		cmsNode = this.cmsNodeDao.selectByPrimaryKey(id);
		if(link){
			cmsNode.setConentList(this.findNodeContentsById(cmsNode.getId()));
		}
		CmsQueryServiceLogger.info("<|>"+className+"<|>findNodeById<|>{pid:"+id+",link:"+link+"}<|>"+cmsNode+"<|>");
		return cmsNode;
	}

	public List<CmsNodeContent> findNodeContentsById(Integer nodeId) {
		List<CmsNodeContent> cmsNodeContentList = null;
		CmsNodeContentExample example = new CmsNodeContentExample();
		example.createCriteria().andNodeIdEqualTo(nodeId);
		cmsNodeContentList = this.cmsNodeContentDAO.selectByExample(example);
		CmsQueryServiceLogger.info("<|>"+className+"<|>findNodeContentsById<|>{nodeId:"+nodeId+"}<|>"+cmsNodeContentList.size()+"<|>");
		return cmsNodeContentList;
	}

}
